<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;
//负责处理用户请求和响应，定义控制器类来处理请求、调用模型、渲染视图。
class ExpertController extends Controller
{
    public function experts()
    {
        $oExperts=Expert::paginate(9);
        return view('front.experts')
            ->with('oExperts',$oExperts);
    }
    public function show_expert($id){

        $expert=Expert::find($id);
        $oExperts=Expert::where('id','!=',$expert->id)->take(3)->get();
        return view('front.expert_info')
            ->with('oExperts',$oExperts)
            ->with('expert',$expert);
    }
    public function create()
    {
        return view('admin.expert_add');
    }
    public function store(Request $request)
    {
        $expert = $request->input();
        $rules=[
            'name'=>'required',
            'department'=>'required',
            'title'=>'required',
            'job'=>'required',
            'hospital'=>'required',
            'introduce'=>'required',
            'education'=>'required',
        ];
        $message=[
            'name.required'=>'专家名不能为空',
            'department.required'=>'科室名不能为空',
            'title.required'=>'职称名不能为空',
            'job.required'=>'职务名不能为空',
            'hospital.required'=>'医院名不能为空',
            'introduce.required'=>'个人简介不能为空',
            'education.required'=>'接受教育不能为空',
        ];
        $validate=Validator::make($expert,$rules,$message);
        if ($validate->fails()){
            return view('admin.fail')->with('message',$message);
        }
        if (isset($expert['photo'])){
            $expert['image']=$expert['photo'];
        }
        else{
            $expert['image']='/assets/test_images/e2.jpg';    //如果没有上传图片，使用默认值
        }
        Expert::create($expert);
        $value='expert';
        return view('admin.success')->with('value',$value);
    }
    public function edit($id)
    {
        $Expert=Expert::find($id);
        return view('admin.expert_edit')
            ->with('Expert',$Expert);
    }
    public function update(Request $request, $id)
    {
        $expert=Expert::find($id);
        $data=$request->all();
        $rules=[
            'name'=>'required',
            'department'=>'required',
            'title'=>'required',
            'job'=>'required',
            'hospital'=>'required',
            'introduce'=>'required',
            'education'=>'required',
        ];
        $message=[
            'name.required'=>'专家名不能为空',
            'department.required'=>'科室名不能为空',
            'title.required'=>'职称名不能为空',
            'job.required'=>'职务名不能为空',
            'hospital.required'=>'医院名不能为空',
            'introduce.required'=>'个人简介不能为空',
            'education.required'=>'接受教育不能为空',
        ];
        $validate=Validator::make($data,$rules,$message);
        if ($validate->fails()){
            return view('admin.fail')->with('message',$message);
        }
        if (isset($data['photo'])){
            $data['image']=$data['photo'];
        }else{
            $data['image']=$expert['image'];
        }
        $expert->update($data);
        $value='expert';
        return view('admin.success')->with('value',$value);
    }
    public function delete($id)
    {
        $experts = Expert::find($id);
        $experts->delete(true);
        $value='expert';
        return view('admin.success')->with('value',$value);
    }
    public function lecture(){
        return view('front.lecture');
    }
    public function list(){
        $experts=Expert::paginate(5);//从数据库中查询专家所有数据给experts变量，利于便利和操作数据。
        return view('admin.expert_list')->with('experts',$experts);//将查询结果传递给视图，with方法将变量绑定到视图，用于渲染页面，展示专家列表。
    }
    public function upload()
    {
        $allow_type = ["image/jpeg", "image/gif", "image/png"];
        if (!in_array($_FILES['upload_file']['type'], $allow_type)) {
            $msg = '图片类型不符合要求';
            return json_encode(['success' => false, 'msg' => $msg]);
        } elseif ($_FILES['upload_file']['size'] > 2000000) {
            $msg = '图片大小不符合要求';
            return json_encode(['success' => false, 'msg' => $msg]);
        } elseif ($_FILES['upload_file']['error'] != 0) {
            $msg = '上传出错';
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {//如果检查都通过执行以下程序
            $src_image = $_FILES['upload_file']['tmp_name'];//获取上传文件临时路径
            $size = getimagesize($_FILES['upload_file']['tmp_name']);//获得图片尺寸，赋给下面
            $src_width = $size[0];
            $src_height = $size[1];
            $tar_width = 400;//像素值
            $tar_height = $tar_width / $src_width * $src_height;//计算目标缩略图的高度
            $tar_thumb = imagecreatetruecolor($tar_width, $tar_height);//将计算出的数据利用函数赋值给tar_thumb
            if ($_FILES['upload_file']['type'] == 'image/jpeg') {//根据上传文件的类型使用函数创建原图的图像资源$src_thumb
                $src_thumb = imagecreatefromjpeg($src_image);
            } elseif ($_FILES['upload_file']['type'] == "image/gif") {
                $src_thumb = imagecreatefromgif($src_image);
            } else {
                $src_thumb = imagecreatefrompng($src_image);
            }
            imagecopyresized($tar_thumb, $src_thumb, 0, 0, 0, 0, $tar_width, $tar_height, $src_width, $src_height);
            $path = './uploads/' . $_FILES['upload_file']['name'];//设置目标路径path为当前目录下文件夹和上传文件原文件名
            imagejpeg($tar_thumb, $path);//使用函数将缩放后的图像保存到目标路径
            return json_encode(['success' => true, 'photo' => '/uploads/' . $_FILES['upload_file']['name']]);//返回json格式的成功响应
        }
    }
}
